package com.robinmouren.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.robinmouren.annotation.AutoFill;
import com.robinmouren.constant.AutoFillConstant;
import com.robinmouren.constant.MessageConstant;
import com.robinmouren.dto.QuotationDto;
import com.robinmouren.dto.QuotationPageDto;
import com.robinmouren.entity.OrderStatus;
import com.robinmouren.enumeration.OperationType;
import com.robinmouren.exception.NullOrderInfoException;
import com.robinmouren.result.Result;
import com.robinmouren.service.impl.OrderServiceImpl;
import com.robinmouren.service.impl.UserServiceImpl;
import com.robinmouren.utils.ListUtils;
import com.robinmouren.vo.OrderStatusStepsVo;
import com.robinmouren.vo.QuotationPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Robinmouren
 * @since 2024-05-12
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private RedisTemplate<String, List<OrderStatus>> redisTemplate;


    /**
     * 询价单分页查询
     * @param pageDto
     * @return
     */
    @PostMapping("/quotation/page")
    public Result<Page<QuotationPageVo>> orderPage(@RequestBody QuotationPageDto pageDto){
      log.info("询价单分页，信息{}",pageDto);

      Page<QuotationPageVo> page = userService.quotationPage(pageDto);
      return Result.success(page);
    }

    /**
     * 获取订单信息 根据订单号
     * @return
     */
    @GetMapping("/getByNumber")
    public Result<QuotationDto> orderGetById(String orderNumber){
        log.info("获取订单信息，订单号id：{}",orderNumber);

        QuotationDto orderInfo = orderService.getByOrderNumber(orderNumber);
        return Result.success(orderInfo);
    }


    /**
     * 获取订单状态流程map集合
     * @return
     */
    @GetMapping("/status/map/get")
    public Result<OrderStatusStepsVo> getOrderStatusMap(String orderNumber){
        log.info("获取订单状态流程map集合，订单号:{}",orderNumber);

        List<OrderStatus> list = redisTemplate.opsForValue().get(orderNumber);
        if(list == null){
            throw new NullOrderInfoException(MessageConstant.ORDER_IS_NULL);
        }
        return Result.success(new OrderStatusStepsVo(ListUtils.fetchStatusInStepsList(list),list));
    }


    /**
     * 订单修改状态
     * @return
     */
    @PutMapping("/status/update")
    public Result<OrderStatusStepsVo> quotationStatusUpdate(String orderNumber,Integer code){
        log.info("询价单修改状态，订单号:{}，要修改的状态值：{}",orderNumber,code);

        orderService.quotationStatusUpdate(orderNumber,code);
        return Result.success();
    }


    /**
     * 询价单添加审核意见
     * @return
     */
    @PutMapping("/opinion")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.CARRIERS_AUDIT)
    public Result<OrderStatusStepsVo> quotationAuditOpinion(String orderNumber,String opinion){
        log.info("询价单添审核意见，订单号:{}，要修改的值：{}",orderNumber,opinion);

        orderService.quotationAuditOpinion(orderNumber,opinion);
        return Result.success();
    }



}
